/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kairos.motion.strategy 
{ 
	import com.bourre.log.PixlibStringifier;
		
	/**
	 * The <code>AdvancedLoopStrategy</code> bring together the <code>LoopStrategy</code>
	 * and the <code>NoLoopStrategy</code>.
	 * 
	 * <p>The <code>isMotionFinished</code> is the same as the <code>NoLoopStrategy</code>
	 * one and the <code>getNextFrame</code> is the <code>LoopStrategy</code> one.</p>
	 * 
	 * @author 	Cédric Néhémie
	 * @see		PlayStrategy
	 */
	public class AdvancedLoopStrategy implements PlayStrategy 
	{
		/**
		 * @inheritDoc
		 */
		public function getInitialFrame ( totalFrames : uint ) : uint
		{
			return 1;
		}
		/**
		 * Returns <code>false</code> when the animation playhead 
		 * go on or further the last frame during the current move.
		 * 
		 * @param	frame		The current frame in the <code>MovieClip</code>
		 * @param	totalFrames	The number of frames in the <code>MovieClip</code>
		 * @param	step		How many frames the playhead have to move.
		 * @return	<code>true</code> if the animation have to stop after the
		 * 			playhead moves, either <code>false</code>
		 */
		public function isMotionFinished(frame : uint, totalFrames : uint, step : uint) : Boolean 
		{
			return frame + step > totalFrames;
		}
		
		/**
		 * Returns the next frame wherein the playhead can move.
		 * 
		 * <p>When the playhead is going to go past the last frame
		 * the animation loop and the playhead follow its move from
		 * first frame.</p>
		 * 
		 * @param	frame		The current frame in the <code>MovieClip</code>
		 * @param	totalFrames	The number of frames in the <code>MovieClip</code>
		 * @param	step		How many frames the playhead have to move.
		 * @return	A valid frame index in the <code>MovieClip</code>
		 */
		public function getNextFrame(frame : uint, totalFrames : uint, step : uint) : uint 
		{
			return isMotionFinished ( frame, totalFrames, step ) ?
						( frame + step ) % totalFrames :
						frame + step;
		}
	
		/**
		 * Return the String representation of the object. 
		 */	
		public function toString () : String
		{
			return PixlibStringifier.stringify( this );
		}
	}
}